<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("./_inc/entityFunction.php");

$errMsg = sxREQ('msg');
$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
$entity = sxREQ('ent');
$dataArea = substr($entity,0,4);

if($entity==''){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".urlencode(_t("missing entity."));
	serverRedir($url);
}
if($dataArea==''){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".urlencode(_t("missing data-area."));
	serverRedir($url);
}
if(!isset($aDataArea[$dataArea])){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".
		urlencode(_t("user diabled viewing '%S'",$dataArea));
	serverRedir($url);
}

if(sxREQ("del") == "yes"){
	//delete entity
	$errMsg .= entDelete();
}
$sSQL = "SELECT e.*, sa.subject_area, se.description AS se_descr, a.description AS a_descr FROM entity e " .
		"	LEFT JOIN subject_area_entity sa ON sa.entity = e.entity " .
		"	LEFT JOIN entity se ON se.entity = e.supertype_entity " .
		"	LEFT JOIN attribute a ON a.entity = e.supertype_entity AND a.attribute = e.discrim_attribute " .
		"  WHERE e.entity='$entity' " .
		" ORDER BY sa.subject_area ";
$objSDAO->query($sSQL,"E");
$rowE = $objSDAO->fetch("E");
if(!$rowE){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".
		urlencode(_t("entity not found",$entity));
	serverRedir($url);
}
$data_area = $rowE["data_area"];
$description = $rowE["description"];
$indef_article = $rowE["indef_article"];
$note = $rowE["note"];
$physical_param = $rowE["physical_param"];
$supertype_entity = $rowE["supertype_entity"];
$se_descr = $rowE["se_descr"];
$discrim_attribute = $rowE["discrim_attribute"];
$a_descr = $rowE["a_descr"];
$discrim_value = $rowE["discrim_value"];

//relationship entity as parent
$sSQLRP = "SELECT r.*, p.description AS parent, c.description AS child, " .
		"      p.indef_article AS parent_indef_article, c.indef_article AS child_indef_article " .
		" FROM relationship r " .
		" INNER JOIN entity p ON p.entity = r.parent_entity " .
		" INNER JOIN entity c ON c.entity = r.child_entity " .
		" WHERE r.parent_entity='$entity'  ORDER BY r.child_entity, r.number";
$objSDAO->query($sSQLRP,"relPar");

//relationship entity as child.
$sSQLRC = "SELECT r.*, p.description AS parent, c.description AS child, " .
		"      p.indef_article AS parent_indef_article, c.indef_article AS child_indef_article " .
		" FROM relationship r " .
		"INNER JOIN entity p ON p.entity = r.parent_entity " .
		"INNER JOIN entity c ON c.entity = r.child_entity " .
		"WHERE r.child_entity='$entity'  ORDER BY r.parent_entity, r.number";
$objSDAO->query($sSQLRC,"relChi");

//subtypes
$sSQLSP = "SELECT e.*, a.description AS a_descr FROM entity e "
	."	LEFT JOIN attribute a ON a.entity = e.supertype_entity AND a.attribute = e.discrim_attribute " 
	." WHERE e.supertype_entity='$entity' "
	." ORDER BY e.discrim_attribute, e.discrim_value";
$objSDAO->query($sSQLSP,"subtypes");

//output HTML
$aOpz = array("kMenu"=>'dam');
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function confDelEntity(){
		if (confirm("Confirm entity delete ?")) {
			document.location.href = 'entityView.php?del=yes<?="&da=$dataArea&ent=$entity&xsn=".XSN?>';
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
$sSQL = "SELECT description FROM data_area WHERE data_area = '$dataArea' ";
$query = $objSDAO->query($sSQL,'da');
if (!$rowDA = $objSDAO->fetch('da')) {
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".urlencode(_t("data-area '%s' not found",$dataArea));
	serverRedir($url);
}
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">");
echo("<tr>" .
		"<td class=\"labl01\" width='10%'>Data-Area: </td>" .
		"<td class=\"text01\" width='90%'><a class='text01' href='../area/dataAreaView.php?da=$dataArea&xsn=".XSN.
			"' title='"._t('Data-Area view')."'>$dataArea</a>' - ".
			sxHE($rowDA['description'])."</td></tr>");
echo("</table>");
$aCmd = array(
					"lstAttr"=>array("pag"=>"../attr/attributeList.php?da=$dataArea&ent=$entity&xsn=".XSN,
										"title"=>_t("list of attriutes"),
										"text"=>_t("attributes"),align=>'L',
										"perm"=>"R"),
					"newRel"=>array("pag"=>"../rela/relationshipIns.php?da=$dataArea&ent=$entity&xsn=".XSN,
										"title"=>_t("New relationship"),
										"text"=>_t("new relationship"),align=>'R',
										"perm"=>"M"),
					"modEnt"=>array("pag"=>"entityMod.php?da=$dataArea&ent=$entity",
										"title"=>_t("change entity"),
										"text"=>_t("change entity"),
										"perm"=>"M"),
					"delEnt"=>array("pag"=>"javascript:confDelEntity()",
										"title"=>_t("delete entity"),
										"text"=>_t("delete entity"),
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
		"<td class=\"titl01\" colspan=\"2\">Entity</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("entity")."</td>" .
			"<td class=\"text01\">".$entity." - ".sxHE($indef_article).' '.sxHE($description)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" style=\"width: 20%;\">"._t("Subject-Areas")."</td>" .
			"<td class=\"text01\" style=\"vertical-align:top;\">");
$sep = '';	
while($rowE) {
	if ($rowE['subject_area']!='') {
		echo $sep.$rowE['subject_area'];
		$sep = ', ';
	}
	$rowE = $objSDAO->fetch("E");
}
echo("</td>" .
		"</tr>\n");
if (!empty($supertype_entity)) {
	echo("<tr>" .
				"<td class=\"labl01\">Supertype-entity</td>" .
				"<td class=\"text01\" style=\"vertical-align:top;\">"
				."<a class=\"text01\" ".goToPag("entityView.php?da=$dataArea&ent="
				.urlencode($supertype_entity)."&xsn=".XSN)." title=\""._t("view supertype-entity")."\">"
				.$supertype_entity."</a> - ".sxHE($se_descr)."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Discriminating attribute</td>" .
				"<td class=\"text01\" style=\"vertical-align:top;\">".sxHE($discrim_attribute." - ".$a_descr)."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Discriminating value</td>" .
				"<td class=\"text01\" style=\"vertical-align:top;\">".sxHE($discrim_value)."</td>" .
			"</tr>\n");
}
if (!empty($note))
	echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\" style=\"vertical-align:top;\"><pre class=\"text01\">".sxWW(sxHE($note))."</pre></td>" .
		"</tr>\n");
if (!empty($physical_param))
	echo("<tr>" .
			"<td class=\"labl01\">"._t("Physical parameters")."</td>" .
			"<td class=\"text01\" style=\"vertical-align:top;\">" .
			"<pre class=\"text01\">".sxWW($physical_param)."</pre></td>" .
		"</tr>\n");
echo("</table>");

//subtypes
if ($row = $objSDAO->fetch("subtypes")) {
	echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr>" .
				"<td class=\"titl01\" colspan=\"3\">Sub-types</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"tabh01\">"._t("Discriminating attribute")."</td>" .
				"<td class=\"tabh01\">"._t("Discriminating value")."</td>" .
				"<td class=\"tabh01\">"._t("Subtype entity")."</td>" .
			"</tr>\n");
	//output subtyping: entity=supertype
	while($row) {
		echo("<tr>" .
				"<td class='text01'>".$row["discrim_attribute"]." - ".sxHE($row["a_descr"])."</a></td>" .				
				"<td class=\"text01\">".sxHE($row["discrim_value"])."</td>" .	
				"<td class=\"text01\"><a class=\"text01\" ".goToPag("entityView.php?da=$dataArea&ent="
				.urlencode($row["entity"])."&xsn=".XSN)." title=\""._t("view subtype-entity")."\">"
				.$row["entity"]."</a> - ".sxHE($row["description"])."</td>" .	
			"</tr>\n");
		$row = $objSDAO->fetch("subtypes");
	}
	echo("</table>");
}
//relazioni
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='4'>"._t("Relationship")."</td>" .
		"</tr>\n");
if ($row = $objSDAO->fetch("relPar")) {
	echo("<tr>" .
			"<td class=\"tabh01\" width='94%'>"._t("As parent")."</td>" .
			"<td class=\"tabh01\" width='2%'>"._t("N.")."</td>" .
			"<td class=\"tabh01\" width='2%'>"._t("IC")."</td>" .
			"<td class=\"tabh01\" width='2%'>"._t("CD")."</td>" .
		"</tr>\n");
}
//output relationship: entity=parent
while($row) {
	echo "<tr>\n<td class=\"text01\">";		
	echo $row["parent_indef_article"]." <b>".sxHE($row["parent"])."</b>"; 
	echo ($row["children_optionality"]=='Y')?' sometimes ':' ';		
	echo "<a class=\"text01\" ".goToPag("../rela/relationshipMod.php?da=$dataArea&ent="
			.urlencode($row["parent_entity"])."&entityChi=".urlencode($row["child_entity"])
			."&number=".$row["number"]."&xsn=".XSN)." title=\""._t("Visualizza la relreqtype")." "
			.$row["number"]."\">";
	echo "<b>".$row["description_parent_child"]."</b>";		
	echo "</a>";
	echo (($row["children_multeplicity"]=='Y')?"&nbsp;one or more than one " :"&nbsp;one");
	echo "<a class=\"text01\" ".goToPag("../enty/entityView.php?da=$dataArea&ent="
			.urlencode($row["child_entity"])."&xsn=".XSN)." title=\""._t("View child entity")."\">";
	echo '<b>'.$row["child"].'</b></a>';		
	echo "</td>\n";
	echo "<td class=\"text01\">".$row["number"]."</td>\n";
	echo "<td class=\"text01\">".$row["integrity_check"]."</td>\n";
	echo "<td class=\"text01\">".$row["cascade_delete"]."</td></tr>\n";
	$row = $objSDAO->fetch("relPar");
}
if ($row = $objSDAO->fetch("relChi")) {
	echo("<tr>" .
			"<td class=\"tabh01\">"._t("As child")."</td>" .
			"<td class=\"tabh01\">"._t("NN")."</td>" .
			"<td class=\"tabh01\"'>"._t("IC")."</td>" .
			"<td class=\"tabh01\">"._t("CD")."</td>" .
		"</tr>\n");
}
//output relationship: entity=child
while($row) {
	echo "<tr>\n<td class=\"text01\">";		
	echo $row["child_indef_article"]." <b>".sxHE($row["child"]).'</b> ';		
	echo ($row["parent_optionality"]=='Y')?' sometimes ':' always ';		
	echo "<a class=\"text01\" ".goToPag("../rela/relationshipMod.php?da=$dataArea&ent="
			.urlencode($row["parent_entity"])."&entityChi=".urlencode($row["child_entity"])
			."&number=".$row["number"]."&xsn=".XSN)." title=\""._t("Visualizza la relreqtype")." "
			.$row["number"]."\">";
	echo "<b>".$row["description_child_parent"]."</b>";		
	echo "</a>&nbsp;".$row["parent_indef_article"]." ";
	echo "<a class=\"text01\" ".goToPag("../enty/entityView.php?da=$dataArea&ent="
			.urlencode($row["parent_entity"])."&xsn=".XSN)." title=\""._t("View parent entity")."\">";
	echo "<b>".$row["parent"]."</b></a></td>\n";
	echo "<td class=\"text01\">".$row["number"]."</td>\n";
	echo "<td class=\"text01\">".$row["integrity_check"]."</td>\n";
	echo "<td class=\"text01\">".$row["cascade_delete"]."</td></tr>\n";
	$row = $objSDAO->fetch("relChi");
}
echo("</table>");
echo("<div id=\"mess01\" class=\"mess01\">$errMsg</div>");
echo(commandsHTML($aCmd));
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function entDelete($bRedir = true){
	global $objSDAO;
	$errMsg = "";
		
	//elimino le relazioni in cui compare la entity
	$sSQL = "DELETE FROM relationship WHERE parent_entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM relationship WHERE child_entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM relationship_key WHERE parent_entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM relationship_key WHERE child_entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM subtyping WHERE supertype_entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM subtyping WHERE subtype_entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	
	$sSQL = "DELETE FROM subject_area_entity WHERE entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	
	//elimino gli attributi
	$sSQL = "DELETE FROM attribute WHERE entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	
	//elimino la entity
	$sSQL = "DELETE FROM entity " .
			" WHERE entity='".sxREQ('ent')."'";	
	$objSDAO->query($sSQL,"del");
	if($bRedir){
		logRecord("510", _t("Entity '%s' deleted",sxREQ('ent')));
		//serverRedir("entityList.php?da=".$_REQUEST["data_area"]."&xsn=".XSN);
		$url = $_SESSION[USEROBJ]->getHistory(-1, true);
		serverRedir($url);
		exit();
	}
}

